from typing import Dict, List
from flask_restful import Resource
from flask import request
from flask import jsonify

from app.dbs.config_db import CofDatabaseConnection
from app.dbs.connect import DatabaseConnection
from app.configure.db_conn_configure import DBConCf


class ShowDatabases(Resource):

    def __init__(self):
        self.host = '127.0.0.1'
        self.user = 'root'
        self.pwd = 'root'
        self.db = 'dcr_web'
        self.local = []
        self.test = []
        self.prod = []
        self.conncof = DBConCf()

    def get(self):
        ip_addr = request.remote_addr
        print(ip_addr)
        data = request.args
        db = data.get('db')
        table = data.get('table')
        env = data.get('env')
        if env == 'prod':
            machine = data.get('machine')
            host, user, pwd, db = self.conncof(env, machine)
            db = CofDatabaseConnection(host=host, user=user, password=pwd, database=db)
        else:    
            db = CofDatabaseConnection(self.host, self.user, self.pwd, self.db)
        databases = db.get_databases()
        print(databases)
        result = {
            'databases': databases,
            'msg': 'success',
            'code':0
        }
        return jsonify(result)
        #host, user, pwd = self.get_db_ip_user_pwd(ip_addr, ven)
        #conn = DatabaseConnection(host, user, pwd, host, db)
        #conn.get_data(table, limit=50, offset=0)

    def get_db_ip_user_pwd(self, ip_addr, ven):
        db = CofDatabaseConnection(self.host, self.user, self.pwd, self.db)
        result = db.get_addr_user_pwd_by_ip(ip_addr)
        if ven == 'test':
            return (result[key] for key in self.test)
        elif ven == 'prod':
            return (result[key] for key in self.prod)
        return (result[key] for key in self.local)
    

    

class ShowTables(Resource):
    # 暂时不通过环境连接对应数据库，默认都连接本地数据库
    def __init__(self):
        self.host = '127.0.0.1'
        self.user = 'root'
        self.pwd = 'root'
        self.db = 'dcr_web'
        self.local = []
        self.test = []
        self.prod = []    
        self.conncof = DBConCf()

    def get(self):
        data = request.args
        env = data.get('env')
        db_id = data.get('db_id')
        if not env or not db_id:
            return {'message': 'Environment or database ID not specified'}, 400
        
        # tables = self.get_tables(db_id, env)
        if env == 'prod':
            machine = data.get('machine')
            host, user, pwd, db = self.conncof(env, machine)
            db = CofDatabaseConnection(host=host, user=user, password=pwd, database=db)
        else:    
            db = CofDatabaseConnection(self.host, self.user, self.pwd, self.db)
        #db = CofDatabaseConnection(self.host, self.user, self.pwd, self.db)
        tables = db.get_tables(db_id)
        result = {
            'tables': tables,
            'msg': 'success',
            'code':0
        }
        return jsonify(result)

    def get_tables(self, db_id, env):

        return []


class GetData(Resource):
    # 暂时不通过环境连接对应数据库，默认都连接本地数据库
    def __init__(self):
        self.host = '127.0.0.1'
        self.user = 'root'
        self.pwd = 'root'
        self.db = 'dcr_web'
        self.local = []
        self.test = []
        self.prod = [] 
        self.conncof = DBConCf()

    def get(self):
        params = request.args
        env = params.get('env')
        db_id = params.get('db_id')
        table = params.get('table')
        page = params.get('page')
        items_per_page = params.get('items_per_page')
        if not env or not db_id:
            return {'message': 'Environment or database ID not specified'}, 400
        
        # tables = self.get_tables(db_id, env)
        if env == 'prod':
            machine = params.get('machine')
            host, user, pwd, db = self.conncof(env, machine)
            db = CofDatabaseConnection(host=host, user=user, password=pwd, database=db)
        else:    
            db = CofDatabaseConnection(self.host, self.user, self.pwd, self.db)
        #db = CofDatabaseConnection(self.host, self.user, self.pwd, self.db)
        data, count = db.get_data(db_id, table, items_per_page, page)
        result = {
            'data': data,
            'count': count,
            'msg': 'success',
            'code':0
        }
        return jsonify(result)

